<?php

namespace app\home\controller;
use app\home\controller\Gettree;
use app\home\model;
use think\Controller;
use think\Db;

set_time_limit(0);

class Api extends Controller
{
    /**
     * 分红（静态）
     */
    public final function chanpin_guding_fenhongs()
    {
        if($_GET['token']!=="Fj56y2D"){
            die;
        }
        $fenhong_time=Db::name('control')
            ->where('id',1)
            ->value('fenhong_control');

        $time=date('Y-m-d',$fenhong_time);
        if($time==date('Y-m-d',time())){
            $this->error('今日已进行操作');
        }
    $up_time=Db::name('control')
                ->where('id',1)
                ->setField('fenhong_control',time());
            if($up_time){
                echo '分红时间已被记录';
            }else{
                echo '分红时间未记录，请联系技术人员';
            }
        $StatusDont = Db::name('buy')
            ->where('b_buy', 1)  //是否支付成功
            ->where('b_status', 0)//订单状态
            ->where('p_nums', '>', 0)//剩余天数
            ->select();
        $data = array();
        Db::startTrans();
        try {

            foreach ($StatusDont as $k => $v) {
                $StatusDont[$k]['p_nums'] = $v['p_nums'] - 1;
                if ($StatusDont[$k]['p_nums'] <= 0) {
                    $StatusDont[$k]['b_status'] = 1;
                }
                //个人
                $val=Db::name('mumber')
                    ->where('uid', $v['uid'])
                    ->setInc('mymoney', $v['p_price']);
                if($val){
                    echo"成功一次</br>";
                }
                //记录
                $data[] = [
                    'trade_text' => '收益' . $v['p_price'],
                    'trade_money' => $v['p_price'],
                    'trade_status' => 4,
                    'uptime' => time(),
                    'uid' => $v['uid'],
                    'trade'=>'购买收益',
                ];
            }
            (new model\Buy)->isUpdate(true)->saveAll($StatusDont);
            Db::name('trade')->insertAll($data);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }
    public function guding(){
        if($_GET['token']!=="Fj56y2D"){
            die;
        }
        $jiangli_time=Db::name('control')
            ->where('id',1)
            ->value('jiangli_control');
        $time=date('Y-m-d',$jiangli_time);
        if($time==date('Y-m-d',time())){
            $this->error('今日已进行操作');
        }
        $up_time=Db::name('control')
            ->where('id',1)
            ->setField('jiangli_control',time());
        //****************************************每日分红********************************************
        $DayTime = $jiangli_time;
        $TomTime = time();
        //当天营业额
        $Money = Db::name('trade')
            ->where('trade_status', 1)
            ->where('uptime', '>', $DayTime)
            ->where('uptime', '<', $TomTime)
            ->sum('trade_money');
        if(!$Money){
            echo '今日投资为0';
            die;
        }
        $data1=[];
        $data2=[];
        $data3=[];
        $uid1= Db::name('mumber_team')
            ->where('tayextension',">=","8")
            ->select();
        if(empty($uid1)){
            echo '暂无人数达到直推6</br>';

        }else{
            //符合推荐6总业绩3000操作
            foreach($uid1 as $v_yeji1){
                $EvryOne1 = sprintf("%.2f", ($Money * (3/ 100)) / count($uid1));//金额计算
                $a1=Db::name('mumber')->where('uid', $v_yeji1['uid'])->setInc('mymoney', $EvryOne1);
                $data1[] = [
                    'uid' => $v_yeji1['uid'],
                    'trade_text'=>'全球分红'.$EvryOne1.'元',
                    'trade_status' => 5,
                    'uptime'=>time(),
                    'trade_money'=>$EvryOne1,
                    'trade'=>'暂无订单',
                ];
            }
            if($a1){echo  '分红6人3%完成！</br>';}
        }
        if(!empty($data1)){
            //3000订单记录
            $jilu11=(new model\Trade)->insertAll($data1);
            if($jilu11){
                echo "添加订单记录6人3%分红完成</br>";
            }
        }
        //6人3000分4%
        $uid2= Db::name('mumber_team')
            ->where('tayextension',">=","12")
            ->where('dangri_yeji',">=","3000")
            ->select();
        if(empty($uid2)){
            echo '暂无人数达到直推6当日业绩3000</br>';

        }else{
            //符合推荐6总业绩3000操作
            foreach($uid2 as $v_yeji2){
                $EvryOne2 = sprintf("%.2f", ($Money * (4/ 100)) / count($uid2));//金额计算
                $a2=Db::name('mumber')->where('uid', $v_yeji2['uid'])->setInc('mymoney', $EvryOne2);
                $data2[] = [
                    'uid' => $v_yeji2['uid'],
                    'trade_text'=>'分红'.$EvryOne2.'元',
                    'trade_status' => 5,
                    'uptime'=>time(),
                    'trade_money'=>$EvryOne2,
                    'trade'=>'暂无订单',
                ];
            }
            if($a2){echo  '分红6人当日业绩30003%完成！</br>';}
        }
        if(!empty($data2)){
            //3000订单记录
            $jilu12=(new model\Trade)->insertAll($data2);
            if($jilu12){
                echo "添加订单记录6人3000业绩2%分红完成</br>";
            }
        }
        //10人10000分2%
        $uid3= Db::name('mumber_team')
            ->where('leiji_zhitui',">=","20")
            ->where('yeji',">=","20000")
            ->select();
        if(empty($uid3)){
            echo '暂无人数达到累计直推20业绩20000</br>';

        }else{
            foreach($uid3 as $v_yeji3){
                $EvryOne3 = sprintf("%.2f", ($Money * (3/ 100)) / count($uid3));//金额计算
                $a3=Db::name('mumber')->where('uid', $v_yeji3['uid'])->setInc('mymoney', $EvryOne3);
                $data3[] = [
                    'uid' => $v_yeji3['uid'],
                    'trade_text'=>'分红'.$EvryOne3.'元',
                    'trade_status' => 5,
                    'uptime'=>time(),
                    'trade_money'=>$EvryOne3,
                    'trade'=>'暂无订单',
                ];
            }
            if($a3){echo  '分红20人业绩200002%完成！</br>';}
        }
        if(!empty($data3)){
            //3000订单记录
            $jilu13=(new model\Trade)->insertAll($data3);
            if($jilu13){
                echo "添加订单记录累计20人200002%分红完成</br>";
            }
        }

        $uid4= Db::name('mumber_team')
            ->where('leiji_zhitui',">=","1000")
            ->where('yeji',">=","50000")
            ->select();
        if(empty($uid4)){
            echo '暂无人数达到累计直推30业绩50000</br>';
        }else{
            foreach($uid4 as $v_yeji4){
                $EvryOne4 = sprintf("%.2f", ($Money * (2/ 100)) / count($uid4));//金额计算
                $a4=Db::name('mumber')->where('uid', $v_yeji4['uid'])->setInc('mymoney', $EvryOne4);
                $data4[] = [
                    'uid' => $v_yeji4['uid'],
                    'trade_text'=>'分红'.$EvryOne4.'元',
                    'trade_status' => 5,
                    'uptime'=>time(),
                    'trade_money'=>$EvryOne4,
                    'trade'=>'暂无订单',
                ];
            }
            if($a4){echo  '分红30人业绩500003%完成！</br>';}
        }
        if(!empty($data4)){
            $jilu14=(new model\Trade)->insertAll($data4);
            if($jilu14){
                echo "添加订单记录累计30人500003%分红完成</br>";
            }
        }

        //直推消除
        $xiaochu=Db::name('mumber_team')
            ->where('tayextension','>',0)
            ->setField('tayextension',0);
        if($xiaochu){
            echo '今日直推清除！';
        }
        $xiaochu2=Db::name('mumber_team')
            ->where('dangri_yeji','>',0)
            ->setField('dangri_yeji',0);
        if($xiaochu2){
            echo '今日业绩清除！';
        }
        //*****************************************每日分红结束****************************************
        if($up_time){
            echo '奖励分红时间已被记录';
        }else{
            echo '奖励分红时间未记录，请联系技术人员';
        }
    }
}
